home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / TransSkel 3.24 / Demos / C Demos / Hello / Hello.c next >
Text File  |  1995-07-08  |  5KB  |  246 lines

  1. /*
  2.  * TransSkel version of "hello, world."
  3.  *
  4.  * Presents a window with "Hello, world." displayed centered in the window
  5.  * when the window is active, and "Goodbye, world." when the window is
  6.  * inactive (e.g., when the application is suspended, or when the "About"
  7.  * alert comes up).
  8.  *
  9.  * 04 Feb 94 Version 1.00, Paul DuBois
  10.  * 21 Feb 94
  11.  * - Updated for TransSkel 3.11.
  12.  * 21 Mar 95
  13.  * - Updated for TransSkel 3.19.
  14.  * 22 Jun 95
  15.  * - Updated for TransSkel 3.22.  The phrase that is initially displayed
  16.  * is set according to whether the application is launched into the
  17.  * foreground or background.  In order for this to work properly, have to
  18.  * install a handler for suspend/resume events to force an activate/deactivate
  19.  * of the window (if launched into the background, no activate event happens
  20.  * on the first resume, for some reason).
  21.  */
  22.  
  23. # include    "TransSkel.h"
  24.  
  25.  
  26. /* strings that appear in window */
  27.  
  28. # define    hello    "\pHello, world."
  29. # define    goodbye    "\pGoodbye, world."
  30.  
  31. /* window positioning ratios */
  32.  
  33. # define    hRatio    FixRatio (1, 2)
  34. # define    vRatio    FixRatio (1, 5)
  35.  
  36. /*
  37.  * Resource numbers
  38.  */
  39.  
  40. # define    aboutAlrtRes    1000                    /* About box */
  41. # define    fileMenuNum        (skelAppleMenuID + 1)    /* File menu */
  42.  
  43.  
  44. /* file menu item numbers */
  45.  
  46. typedef enum
  47. {
  48.     quit = 1
  49. };
  50.  
  51.  
  52. static MenuHandle    fileMenu;
  53.  
  54. /* current phrase */
  55.  
  56. static StringPtr    phrase;
  57.  
  58.  
  59. /* -------------------------------------------------------------------- */
  60. /*                        Menu handling procedures                        */
  61. /* -------------------------------------------------------------------- */
  62.  
  63.  
  64. /*
  65.  * Handle selection of "About Hello..." item from Apple menu
  66.  */
  67.  
  68. static pascal void
  69. DoAppleMenu (short item)
  70. {
  71.     (void) SkelAlert (aboutAlrtRes, SkelDlogFilter (nil, true),
  72.                                         skelPositionOnParentDevice);
  73.     SkelRmveDlogFilter ();
  74. }
  75.  
  76.  
  77. /*
  78.  * Process selection from File menu.
  79.  */
  80.  
  81. static pascal void
  82. DoFileMenu (short item)
  83. {
  84.     switch (item)
  85.     {
  86.     case quit:
  87.         SkelStopEventLoop ();
  88.         break;
  89.     }
  90. }
  91.  
  92.  
  93. /*
  94.  * Initialize menus.  Tell TransSkel to process the Apple menu
  95.  * automatically, and associate the proper procedure with the
  96.  * File menu.
  97.  *
  98.  * \311 is the ellipsis character.
  99.  */
  100.  
  101. static void
  102. SetupMenus (void)
  103. {
  104.     SkelApple ("\pAbout Hello\311", DoAppleMenu);
  105.     fileMenu = NewMenu (fileMenuNum, "\pFile");
  106.     AppendMenu (fileMenu, "\pQuit/Q");
  107.     (void) SkelMenu (fileMenu, DoFileMenu, nil, false, false);
  108.  
  109.     DrawMenuBar ();
  110. }
  111.  
  112.  
  113. /* -------------------------------------------------------------------- */
  114. /*                    Window handling procedures                            */
  115. /* -------------------------------------------------------------------- */
  116.  
  117.  
  118. /*
  119.  * Draw grow box of window in lower right hand corner
  120.  */
  121.  
  122.  
  123. static void
  124. DrawGrowBox (WindowPtr w)
  125. {
  126. RgnHandle    oldClip;
  127. Rect        r;
  128.  
  129.     r = w->portRect;
  130.     r.left = r.right - 15;        /* draw only in corner */
  131.     r.top = r.bottom - 15;
  132.     oldClip = NewRgn ();
  133.     GetClip (oldClip);
  134.     ClipRect (&r);
  135.     DrawGrowIcon (w);
  136.     SetClip (oldClip);
  137.     DisposeRgn (oldClip);
  138. }
  139.  
  140.  
  141. static pascal void
  142. Update (Boolean resized)
  143. {
  144. WindowPtr    w;
  145. Rect        r;
  146. short        h, v;
  147.  
  148.     GetPort (&w);
  149.     r = w->portRect;
  150.     EraseRect (&r);
  151.     h = (r.left + r.right - StringWidth (phrase)) / 2;
  152.     v = (r.top + r.bottom) / 2;
  153.     MoveTo (h, v);
  154.     DrawString (phrase);
  155.     DrawGrowBox (w);
  156. }
  157.  
  158.  
  159. /*
  160.  * Normally the grow icon is drawn on a change of activation state, but since
  161.  * the entire portRect is invalidated here, everything will be redrawn by
  162.  * Update(), anyway, including the grow box.  So don't bother here.
  163.  */
  164.  
  165. static pascal void
  166. Activate (Boolean active)
  167. {
  168. WindowPtr    w;
  169.  
  170.     GetPort (&w);
  171.     phrase = (active ? hello : goodbye);
  172.     InvalRect (&w->portRect);
  173. }
  174.  
  175.  
  176. static pascal void
  177. Clobber (void)
  178. {
  179. WindowPtr    w;
  180.  
  181.     GetPort (&w);
  182.     DisposeWindow (w);
  183. }
  184.  
  185.  
  186. static pascal void
  187. DoSuspendResume (Boolean inForeground)
  188. {
  189.     SkelActivate (FrontWindow (), inForeground);
  190. }
  191.  
  192.  
  193. /*
  194.  * Create window  and install handler for it.  Mouse and key clicks are
  195.  * ignored.  There is no close proc since the window doesn't have a
  196.  * close box.  There is no idle proc since nothing is done while the
  197.  * window is in front (all the things that are done are handled by
  198.  * TransSkel).
  199.  */
  200.  
  201. static void
  202. WindInit (void)
  203. {
  204. WindowPtr    w;
  205. Rect    bounds;
  206.  
  207.     phrase = (SkelQuery (skelQInForeground) ? hello : goodbye);
  208.  
  209.     SetRect (&bounds, 0, 0, 200, 100);
  210.     if (SkelQuery (skelQHasColorQD))
  211.     {
  212.         w = NewCWindow (nil, &bounds, "\pHowdy", false,
  213.                         documentProc+8, (WindowPtr) -1, false, 0L);
  214.     }
  215.     else
  216.     {
  217.         w = NewWindow (nil, &bounds, "\pHowdy", false,
  218.                         documentProc+8, (WindowPtr) -1, false, 0L);
  219.     }
  220.     SkelPositionWindow (w, skelPositionOnMainDevice,
  221.                                     FixRatio (1, 2), FixRatio (1,5));
  222.     (void) SkelWindow (w, nil, nil, Update, Activate, nil,
  223.                     Clobber, nil, false);
  224.     TextFont (0);    /* select system font in default size */
  225.     TextSize (0);
  226.     SelectWindow (w);
  227.     ShowWindow (w);
  228. }
  229.  
  230.  
  231. /* -------------------------------------------------------------------- */
  232. /*                                    Main                                */
  233. /* -------------------------------------------------------------------- */
  234.  
  235.  
  236. void
  237. main (void)
  238. {
  239.     SkelInit ((SkelInitParamsPtr) nil);        /* initialize */
  240.     SetupMenus ();                            /* install menu handlers */
  241.     WindInit();                                /* install window handler */
  242.     SkelSetSuspendResume (DoSuspendResume);
  243.     SkelEventLoop ();                        /* loop 'til Quit selected */
  244.     SkelCleanup ();                            /* clean up */
  245. }
  246.